package Jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

/**
 * Document：文档对象。代表内存中的dom树
 *
 * 获取Element对象
 * - getElementById(String id):
 * - getElementsByTag(String tagName):根据标签名称获取元素对象集合
 * - getElementsByAttribute(String key):根据属性名称获取元素对象集合
 * - getElementsByAttributeValue(String key,String value):根据对应的属性名和属性值获取元素对象集合
 *
 * @author Mc.Chai
 * @program: JavaWeb
 * @create 2022-03-17-21:10 21:10
 */
public class DocumentDemo {
    public static void main(String[] args) throws IOException, URISyntaxException {
        String path = DocumentDemo.class.getClassLoader().getResource("student.xml").toURI().getPath();
        Document document= Jsoup.parse(new File(path), "utf-8");
        //getElementById(String id)
        Element elementById = document.getElementById("1");
        System.out.println(elementById);
        System.out.println("------------------");
        //getElementsByTag(String tagName)
        Elements names = document.getElementsByTag("name");
        System.out.println(names);
        System.out.println("-------------------------");
        //getElementsByAttribute(String key)
        Elements id = document.getElementsByAttribute("id");
        System.out.println(id);
        System.out.println("-----------------------");
        //getElementsByAttributeValue(String key,String value)
        Elements id2 = document.getElementsByAttributeValue("id", "2");
        System.out.println(id2);

    }
}
